package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.OrderdetailetDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.po.OrderDetailet;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class OrderdetailetDaoImpl implements OrderdetailetDao {
    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;

    /**
     *         //        INSERT INTO users (username, email, birthdate, is_active)
     *         //        VALUES
     *         //                ('test1', 'test1@runoob.com', '1985-07-10', true),
     *         //                ('test2', 'test2@runoob.com', '1988-11-25', false),
     *         //        ('test3', 'test3@runoob.com', '1993-05-03', true);
     * @param list
     * @return
     * @throws Exception
     */
    @Override
    public int saveBatch(List<OrderDetailet> list) throws Exception {
        //创建一个StringBuffer对象完成动态SQL
        StringBuffer stringBuffer = new StringBuffer("INSERT INTO orderdetailet (orderId, foodId,quantity) VALUES");
        //遍历list集合
        for (OrderDetailet orderDetailet:list) {
            stringBuffer.append("("+orderDetailet.getOrderId()+","+orderDetailet.getFoodId()+","+orderDetailet.getQuantity()+"),");
        }
        //将sql语句中最后的逗号截取
        String sql = stringBuffer.toString().substring(0,stringBuffer.toString().length()-1);
        int result = 0;
        try {
            //获取连接
            con = DBUtil.getConnection();
            //通过调用Connection接口中的方法prepareStatement(sql);获取PreparedStatement对象
            pst = con.prepareStatement(sql);
            //执行查询
            result = pst.executeUpdate();
        }finally {
            DBUtil.close(rs,pst);
        }
        return result;
    }

    @Override
    public List<OrderDetailet> listOrderDetailetByOrderId(int orderId)throws Exception {
        List<OrderDetailet> list = new ArrayList<>();
        StringBuffer sql = new StringBuffer();
        sql.append(" select o.*, ");
        sql.append("        f.foodId ffoodId, ");
        sql.append("        f.foodName ffoodName, ");
        sql.append("        f.foodPrice ffoodPrice ");
        sql.append(" from OrderDetailet o left join food f on o.foodId=f.foodId ");
        sql.append(" where o.orderId=? ");
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql.toString());
            pst.setInt(1,orderId);
            rs = pst.executeQuery();
            while(rs.next()) {
                OrderDetailet od = new OrderDetailet();
                od.setOdId(rs.getInt("odId"));
                od.setOrderId(rs.getInt("orderId"));
                od.setFoodId(rs.getInt("foodId"));
                od.setQuantity(rs.getInt("quantity"));

                Food food = new Food();
                food.setFoodId(rs.getInt("ffoodId"));
                food.setFoodName(rs.getString("ffoodName"));
                food.setFoodPrice(rs.getDouble("ffoodPrice"));
                //设置关联关系
                od.setFood(food);
                //将OrderDetailet保存到集合中
                list.add(od);
            }
        }finally {
            DBUtil.close(pst);
        }
        return list;
    }
}
